import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import MusicCreate from "../components/music/MusicCreate.vue"
import MusicList from "../components/music/MusicList.vue"
import MusicsCreate from "../components/musicList/MusicsCreate.vue"
import MusicsList from "../components/musicList/MusicsList.vue"
import NewsCreate from "../components/news/NewsCreate.vue"
import NewsList from "../components/news/NewsList.vue"
import AdminCreate from "../components/admin/AdminCreate.vue"
import AdminList from "../components/admin/AdminList.vue"
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { isPublic: true }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [{
      path: "/music/create",
      component: MusicCreate,
    },

    {
      path: "/music/list",
      component: MusicList,
    },
    {
      path: "/music/edit/:id",
      component: MusicCreate,
      props: true
    },
    {
      path: "/musicList/create",
      component: MusicsCreate,
    },

    {
      path: "/musicList/list",
      component: MusicsList,
    },
    {
      path: "/musicList/edit/:id",
      component: MusicsCreate,
      props: true
    },
    {
      path: "/news/create",
      component: NewsCreate,
    },
    {
      path: "/news/list",
      component: NewsList,
    },
    {
      path: "/news/edit/:id",
      component: NewsCreate,
      props: true
    },
    {
      path: "/admin/create",
      component: AdminCreate,
    },
    {
      path: "/admin/list",
      component: AdminList,
    },
    {
      path: "/admin/edit/:id",
      component: AdminCreate,
      props: true
    },
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//不是公开访问的并且token不存在，跳转登录界面
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  // console.log(to.meta);
  next()
})

export default router
